home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsI / Src / AALines / utah.c < prev    next >
Text File  |  1992-06-16  |  5KB  |  232 lines

  1. /*
  2.     file:        utah.c
  3.     description:    interface to Utah RLE toolkit
  4.     author:        A. T. Campbell
  5.     date:        October 27, 1989
  6. */
  7.  
  8. #ifndef lint
  9. static char    sccsid[] = "%W% %G%";        /* SCCS info */
  10. #endif lint
  11.     
  12. #include <math.h>
  13. #include <stdio.h>
  14. #ifdef sequent
  15. #include <strings.h>
  16. #else
  17. #include <string.h>
  18. #endif
  19. #include "utah.h"
  20.  
  21. /******************************************************************************/
  22.  
  23. /* return values */
  24. extern void    free();
  25. extern char    *malloc();
  26.  
  27. /******************************************************************************/
  28.  
  29. utah_read_close(ufp)
  30. UTAH_FILE    *ufp;
  31. {
  32.     return(0);
  33. }
  34.  
  35. /******************************************************************************/
  36.  
  37. UTAH_FILE *
  38. utah_read_init(fname, ht, wd)
  39.  
  40. char    *fname;
  41. int    *ht, *wd;
  42. {
  43.     FILE        *fp;
  44.     UTAH_FILE    *ufp;
  45.  
  46.     /* open output stream */
  47.     if (!strcmp(fname, ""))
  48.         fp = stdin;
  49.     else {
  50.         if ((fp = fopen(fname, "r")) == NULL)
  51.         return(NULL);
  52.     }
  53.  
  54.      /* change the default sv_globals struct to match what we need */
  55.     ufp = (UTAH_FILE *) malloc(sizeof(UTAH_FILE));
  56.     *ufp = sv_globals;
  57.     ufp->svfb_fd = fp;
  58.  
  59.     /* read the header in the input file */
  60.       if (rle_get_setup(ufp) != 0)
  61.         return(NULL);
  62.  
  63.     /* get image size */
  64.     *wd = ufp->sv_xmax - ufp->sv_xmin + 1;
  65.     *ht = ufp->sv_ymax - ufp->sv_ymin + 1;
  66.  
  67.     /* normal termination */
  68.     return(ufp);
  69. }
  70.  
  71. /******************************************************************************/
  72.  
  73. utah_read_pixels(ufp, pixels)
  74.  
  75. UTAH_FILE     *ufp;
  76. unsigned char    pixels[][3];
  77. {
  78.     static unsigned    n = 0;
  79.     static unsigned char    *r = NULL, *g = NULL, *b = NULL;
  80.     int        i, width;
  81.  
  82.     /* allocate storage */
  83.     width = ufp->sv_xmax + 1;
  84.     if (width > n) {
  85.         if (n > 0) {
  86.             free((char *)r);
  87.             free((char *)g);
  88.             free((char *)b);
  89.         }
  90.         n = width;
  91.         r = (unsigned char *) malloc(n * sizeof(unsigned char));
  92.         g = (unsigned char *) malloc(n * sizeof(unsigned char));
  93.         b = (unsigned char *) malloc(n * sizeof(unsigned char));
  94.     }
  95.  
  96.     /* read this row */
  97.     utah_read_rgb(ufp, r, g, b);
  98.  
  99.     /* convert to pixels */
  100.     for (i = 0; i < width; i++) {
  101.         pixels[i][0] = r[i];
  102.         pixels[i][1] = g[i];
  103.         pixels[i][2] = b[i];
  104.     }
  105.  
  106.     return(0);
  107. }
  108.  
  109. /******************************************************************************/
  110.  
  111. utah_read_rgb(ufp, r, g, b)
  112.  
  113. UTAH_FILE    *ufp;
  114. unsigned char    r[], g[], b[];
  115. {
  116.     rle_pixel    *rows[3];
  117.  
  118.     /* set color channels */
  119.     rows[0] = r;
  120.     rows[1] = g;
  121.     rows[2] = b;
  122.  
  123.     /* read this row */
  124.     rle_getrow(ufp, rows);
  125.     return(0);
  126. }
  127.  
  128. /******************************************************************************/
  129.  
  130. utah_write_close(ufp)
  131.  
  132. UTAH_FILE    *ufp;
  133. {
  134.     if (!ufp) return(-1);
  135.     sv_puteof(ufp);
  136.     return(0);
  137. }
  138.  
  139. /******************************************************************************/
  140.  
  141. UTAH_FILE *
  142. utah_write_init(fname, ht, wd)
  143.  
  144. char    *fname;
  145. int    ht, wd;
  146. {
  147.     FILE        *fp;
  148.     UTAH_FILE    *ufp;
  149.  
  150.     /* open output stream */
  151.     if (!strcmp(fname, ""))
  152.         fp = stdout;
  153.     else {
  154.         if ((fp = fopen(fname, "w")) == NULL)
  155.         return(NULL);
  156.     }
  157.  
  158.      /* change the default sv_globals struct to match what we need */
  159.     ufp = (UTAH_FILE *) malloc(sizeof(UTAH_FILE));
  160.     *ufp = sv_globals;
  161.     ufp->svfb_fd = fp;
  162.     ufp->sv_xmax = wd - 1;
  163.     ufp->sv_ymax = ht - 1;
  164.     ufp->sv_alpha = 0;    /* No coverage (alpha) */
  165.  
  166.     /* create the header in the output file */
  167.       sv_setup(RUN_DISPATCH, ufp);
  168.  
  169.     /* normal termination */
  170.     return(ufp);
  171. }
  172.  
  173. /******************************************************************************/
  174.  
  175. utah_write_pixels(ufp, pixels)
  176.  
  177. UTAH_FILE    *ufp;
  178. unsigned char    pixels[][3];
  179. {
  180.     static unsigned    n = 0;
  181.     static unsigned char    *r = NULL, *g = NULL, *b = NULL;
  182.     int        i, width;
  183.  
  184.     /* allocate storage */
  185.     width = ufp->sv_xmax + 1;
  186.     if (width > n) {
  187.         if (n > 0) {
  188.             free((char *)r);
  189.             free((char *)g);
  190.             free((char *)b);
  191.         }
  192.         n = width;
  193.         r = (unsigned char *) malloc(n * sizeof(unsigned char));
  194.         g = (unsigned char *) malloc(n * sizeof(unsigned char));
  195.         b = (unsigned char *) malloc(n * sizeof(unsigned char));
  196.     }
  197.  
  198.     /* convert to color channels */
  199.     for (i = 0; i < width; i++) {
  200.         r[i] = pixels[i][0];
  201.         g[i] = pixels[i][1];
  202.         b[i] = pixels[i][2];
  203.     }
  204.  
  205.     /* write this row */
  206.     utah_write_rgb(ufp, r, g, b);
  207.     return(0);
  208. }
  209.  
  210. /******************************************************************************/
  211.  
  212. utah_write_rgb(ufp, r, g, b)
  213.  
  214. UTAH_FILE    *ufp;
  215. unsigned char    r[], g[], b[];
  216. {
  217.     rle_pixel    *rows[3];
  218.     int        width;
  219.  
  220.     /* set color channels */
  221.     rows[0] = r;
  222.     rows[1] = g;
  223.     rows[2] = b;
  224.  
  225.     /* write this row */
  226.     width = ufp->sv_xmax - ufp->sv_xmin + 1;
  227.     sv_putrow(rows, width, ufp);
  228.     return(0);
  229. }
  230.  
  231. /******************************************************************************/
  232.